<?php
namespace Admin\Controller;
class IndexController extends AuthController {
    public function index(){
       $this->display();
    }

    //安全退出
    public function quite()
    {
        session('blog_admin',null);
        $this->redirect('Login/login');
    }
    /*
     * 编辑管理员信息*/
    public function profile()
    {

        $this->page_title="باشقۇرغۇچى ئارخىپ باشقۇرۇش";
        $this->admin=M('Admin')->where(array('id'=>session('blog_admin.id')))->find();
        $this->display();
    }
    /*
     * */
    public function saveHandler(){
        $post=I('post.');
        if($post['password'])
        {
            $rules=[
              ['admin_name','require','管理员名不能为空'],
                ['password','require','密码不能为空'],
                ['re_password','password','两次密码不一致',0,'confirm']
            ];
            if(!M('Admin')->validate($rules)->create($post)) $this->error(M('Admin')->getError());
            $old_password=M('Admin')->where(array('id'=>session('blog_admin.id')))->getField('password');
            if(md5($post['old_password']) !=$old_password) $this->error('原密码不正确');
            session('blog_admin',null);
            $data=[
              'id'=>$post['id'],
                'admin_name'=>$post['admin_name'],
                'password'=>md5($post['password'])
            ];
        }else{
            $data=[
            'id'=>$post['id'],
            'admin_name'=>$post['admin_name']
                ];
        }
        if(!M('Admin')->save($data)) $this->error('更新失败');
        $this->success('更新成功',U('Index/index'));
    }
}